package com.fighYo.edu.controller;


import com.fighYo.commonutils.R;
import com.fighYo.edu.entity.EduChapter;
import com.fighYo.edu.entity.vo.ChapterVo;
import com.fighYo.edu.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2021-08-24
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @GetMapping("getChapter/{courseId}")
    public R getChapters(@PathVariable("courseId") String courseId) {
        List<ChapterVo> chapters = eduChapterService.getChapterByCourseId(courseId);
        return R.ok().data("items", chapters);
    }

    @ApiOperation(value = "增加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        eduChapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable("id") String id) {
        boolean result = eduChapterService.removeChapterById(id);
        return result ? R.ok() : R.error().message("删除成功");
    }

    @ApiOperation(value = "根据ID修改章节")
    @PutMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter) {
        eduChapterService.updateById(chapter);
        return R.ok();
    }

    @GetMapping("getChapterById/{id}")
    public R getChapterById(@PathVariable("id") String id) {
        EduChapter chapter = eduChapterService.getById(id);
        return R.ok().data("item", chapter);
    }
}

